home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / EmacsTeX / Emacs-3.0.1 / Source / keycode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  2.9 KB  |  141 lines

  1. /* Grab a copy of the keyboard translation table so we can find the charCode
  2.    for a given keyCode, ignoring the Alternate key.
  3.  
  4.    For legal stuff see the file COPYRIGHT.  */
  5.  
  6. #include <dpsclient/dpsclient.h>
  7. #include <drivers/event_status_driver.h>
  8. #include <libc.h>
  9. #include <limits.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/file.h>
  13. #include <sys/types.h>
  14.  
  15. static char keycode[256][4];
  16. static havekeymap = 0;
  17.  
  18. /* Given the char mask MASK for a key return the number of chars generated
  19.    by that key.  */
  20. static int
  21. mask_skip (char mask)
  22. {
  23.   int i, skip;
  24.  
  25.   if (mask == -1)
  26.     return 0;
  27.  
  28.   for (i = CHAR_BIT, skip = 1; i > 0; i--, mask >>=1)
  29.     if (mask & 01)
  30.       skip <<= 1;
  31.   return skip;
  32. } /* mask_skip */
  33.  
  34. /* Get the current system keymap and put it into an understandable array
  35.    KEYCODE.  */
  36. void
  37. kc_init (void)
  38. {
  39.   int kc, kcs, mask, skip;
  40.   NXEventHandle handle;
  41.   NXKeyMapping map;
  42.   char evfs, nks;
  43.   char *mapping;
  44.  
  45.   /* Get the current keymap from the Event Status Driver.  */
  46.   handle = NXOpenEventStatus ();
  47.   if (!handle)
  48.     {
  49.       perror ("Emacs: NXOpenEventStatus");
  50.       return;
  51.     }
  52.   map.size = NXKeyMappingLength (handle);
  53.   map.mapping = malloc (map.size);
  54.   if (!NXGetKeyMapping (handle, &map))
  55.     {
  56.       perror ("Emacs: NXGetKeyMapping");
  57.       return;
  58.     }
  59.   NXCloseEventStatus (handle);
  60.  
  61.   /* Check mapping format. */
  62.   if (map.mapping[0] || map.mapping[1])
  63.     {
  64.       fprintf(stderr, "Emacs: unknown keyboard map format.\n");
  65.       return;
  66.     }
  67.   mapping = map.mapping + 2;
  68.  
  69.   /* Skip event flags. */
  70.   for (evfs = *mapping++; evfs > 0; evfs--)
  71.     {
  72.       /* Skip bit number. */
  73.       mapping++;
  74.       /* Number of keys assigned to bit. */
  75.       nks = *mapping++;
  76.       /* Skip key codes. */
  77.       mapping += nks;
  78.     }
  79.  
  80.   /* kcs = number of key defs.  */
  81.   kcs = (unsigned char) *mapping++;
  82.  
  83.   for (kc = 0; kc < kcs; kc++)
  84.     {
  85.       /* Get mask */
  86.       mask = *mapping++;
  87.       skip = mask_skip (mask);
  88.  
  89.       keycode[kc][0] = mapping[1];
  90.       /* Shift key */
  91.       if (mask & 3)
  92.     {
  93.       /* Assumes only one bit will be set */
  94.       keycode[kc][1] = mapping[3];
  95.       /* Shift key & Control key */
  96.       if (mask & 4)
  97.         {
  98.           keycode[kc][2] = mapping[5];
  99.           keycode[kc][3] = mapping[7];
  100.         }
  101.       else
  102.         {
  103.           keycode[kc][2] = keycode[kc][0];
  104.           keycode[kc][3] = keycode[kc][1];
  105.         }
  106.     }
  107.       else
  108.     {
  109.       keycode[kc][1] = keycode[kc][0];
  110.       /* Control key */
  111.       if (mask & 4)
  112.         {
  113.           keycode[kc][2] = keycode[kc][3] = mapping[3];
  114.         }
  115.       else
  116.         {
  117.           keycode[kc][2] = keycode[kc][3] = keycode[kc][0];
  118.         }
  119.     }
  120.       /* Skip defs. */
  121.       mapping += 2 * skip;
  122.     }
  123.   havekeymap = 1;
  124. } /* kc_init */
  125.  
  126. int
  127. kc_keyforcode (int code, int flags)
  128. {
  129.   int modifier = 0;
  130.  
  131.   if (!havekeymap)
  132.     return -1;
  133.  
  134.   if (flags & NX_CONTROLMASK)
  135.     modifier += 2;
  136.   if (flags & NX_SHIFTMASK)
  137.     modifier++;
  138.  
  139.   return keycode[code][modifier];
  140. } /* kc_keyforcode */
  141.